# -*- coding: utf-8 -*-
# created on 2016/11/10

from sympy import sympify
from mathsolver.functions.base import BaseFunction
from mathsolver.functions.hanshu.jiou_dingyi import dairu_jiou


# TODO: objects 变动
# 函数奇偶性与单调性解不等式

class JiOuBuDengShi(BaseFunction):
    def solver(self, *args):
        func = args[0]
        expr = args[-1].sympify()

        func_oi = {}
        # f(a) = b
        if len(args) == 3:
            func2 = args[1]
            func_oi = {int(func2.expression): sympify("%s(%s)" % (func2.name, func2.var))}
        # 如果是奇函数， f(0) = 0
        if func.jiou.is_ji:
            func_oi.update({0: sympify("%s(%s)" % (func.name, 0))})

        # 把 f(a) + f(b) < 0 表达式转化成 f(a) < -f(b) 的形式
        (left, right), relation = expr.args, expr.func
        if left.is_Add:
            additional = left.args[0]
            left, right = left - additional, right - additional
        elif right.is_Add:
            additional = right.args[0]
            right, left = right - additional, left - additional

        # 如果表达式是常数a，用 f(b) 代替，a = f(b)
        if left.is_Number:
            left = left.subs(func_oi)
        elif right.is_Number:
            right = right.subs(func_oi)

        # 如果是，代入 f(x) = f(-x) 或者 f(x) = -f(-x)
        new_expr = relation(*(left, right)).canonical
        left, right = new_expr.args
        relation = new_expr.func

        jiou_eq = func.jiou.property
        if left.is_Mul and left.args[0] == -1:
            f_a = left.args[1]
            new_f_a = dairu_jiou(f_a, jiou_eq)
            left = left.subs(f_a, new_f_a)
        elif right.is_Mul and right.args[0] == -1:
            f_a = right.args[1]
            new_f_a = dairu_jiou(f_a, jiou_eq)
            right = right.subs(f_a, new_f_a)

        # TODO 下一步如何推到有待确定

        return self


if __name__ == '__main__':
    pass
